package com.fixme4trade.view;

import javax.inject.Inject;

import br.gov.frameworkdemoiselle.annotation.Name;
import br.gov.frameworkdemoiselle.annotation.PreviousView;
import br.gov.frameworkdemoiselle.stereotype.ViewController;
import br.gov.frameworkdemoiselle.template.AbstractEditPageBean;
import br.gov.frameworkdemoiselle.transaction.Transactional;
import com.fixme4trade.business.UserBC;
import com.fixme4trade.domain.User;

@Name("user")
@ViewController
@PreviousView("/user_list.xhtml")
public class UserEditMB extends AbstractEditPageBean<User, Long> {

	private static final long serialVersionUID = 1L;
	
	private String password1;
	
	private String password2;
	
	@Inject
	private UserBC bc;

	@Override
	@Transactional
	public String delete() {
		this.bc.delete(getId());
		return getPreviousView();
	}

	@Override
	@Transactional
	public String insert() {
		
		passwordUpdate();
		
		this.bc.insert(getBean());
		return getPreviousView();
	}

	private void passwordUpdate() {
		if (password1 != null && password2 != null && password1.equals(password2)) {
			
			getBean().setPassword(password1);
		}
	}

	@Override
	@Transactional
	public String update() {
		this.bc.update(getBean());
		return getPreviousView();
	}

	@Override
	protected void handleLoad() {
		setBean(this.bc.load(getId()));
	}

	public String getPassword1() {
		return password1;
	}

	public void setPassword1(String password1) {
		this.password1 = password1;
	}

	public String getPassword2() {
		return password2;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}
}
